﻿using diary.DAL;
using diary.POCO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace diary
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public enum VIEWS { TODAY, YESTERDAY, TWODAYS, MONTH, BESTYEAR, XML, STATS, BESTMONTH, OVERVIEW }

        private DiaryLoader _diaryLoader;
        private DiarySaver _diarySaver;
        private Diary _diary;

        private DayView _dayView;
        private DayView _yesterdayView;
        private DayView _twodaysView;
        private MonthView _monthView;
        private YearView _yearView;
        private XmlView _xmlView;
        private BestView _bestMonthView;
        private BestView _bestYearView;
        private StatsView _statsView;
        private OverviewView _overviewView;

        public MainWindow()
        {
            InitializeComponent();

        }

        private void _dayView_OnSaveCommand(object sender)
        {
            _diarySaver.SaveDiary(_diary);
        }

        private void _yesterdayView_OnSaveCommand(object sender)
        {
            _diarySaver.SaveDiary(_diary);
        }

        private void _twodaysView_OnSaveCommand(object sender)
        {
            _diarySaver.SaveDiary(_diary);
        }

        private void SwitchView(VIEWS view)
        {
            MainContent.Children.Clear();
            UserControl selectedView = null;
            switch(view)
            {
                case VIEWS.TODAY:
                    selectedView = _dayView;
                    break;
                case VIEWS.YESTERDAY:
                    selectedView = _yesterdayView;
                    break;
                case VIEWS.TWODAYS:
                    selectedView = _twodaysView;
                    break;
                case VIEWS.MONTH:
                    selectedView = _monthView;
                    break;
                case VIEWS.BESTYEAR:
                    selectedView = _bestYearView;
                    break;
                case VIEWS.XML:
                    selectedView = _xmlView;
                    break;
                case VIEWS.BESTMONTH:
                    selectedView = _bestMonthView;
                    break;
                case VIEWS.OVERVIEW:
                    selectedView = _overviewView;
                    break;
            }

            MainContent.Children.Add(selectedView);
            Grid.SetRow(selectedView, 0);
            Grid.SetColumn(selectedView, 1);
        }

        private void SetDiaryCurrentDay(DateTime specificDate)
        {
            Day day = null;
            Year year = null;
            Month month = null;
            try
            {
                year = _diary.Years.First(s => s.CurrentYear == specificDate.Year);
                if (year != null)
                {
                    month = year.Months.First(s => s.CurrentMonth == specificDate.Month);
                    if (month!=null)
                    {
                        day = month.Days.First(s => s.CurrentDay == specificDate.Day);
                    }
                }

            }
            catch
            {
                if (year == null)
                { 
                    year = new Year();
                    year.CurrentYear = specificDate.Year;
                    _diary.Years.Add(year);
                }
                if (month == null)
                {
                    month = new Month();
                    month.CurrentMonth = specificDate.Month;
                    year.Months.Add(month);
                }

                day = new Day();
                day.CurrentDay = specificDate.Day;
                day.CurrentDate = specificDate;
                month.Days.Add(day);
            }
            _diary.CurrentDay = day;
            _diary.CurrentMonth = month;
            _diary.CurrentYear = year;
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            _diaryLoader = new DiaryLoader();
            _diarySaver = new DiarySaver();

            _dayView = new DayView();
            _dayView.OnSaveCommand += _dayView_OnSaveCommand;

            _yesterdayView = new DayView();
            _yesterdayView.OnSaveCommand +=_yesterdayView_OnSaveCommand;

            _twodaysView = new DayView();
            _twodaysView.OnSaveCommand += _twodaysView_OnSaveCommand;

            
            _monthView = new MonthView();
            _yearView = new YearView();
            _xmlView = new XmlView();
            _statsView = new StatsView();
            _overviewView = new OverviewView();
            _bestMonthView = new BestView();
            _bestYearView = new BestView();
            _monthView.OnSelectedDate += _monthView_OnSelectedDate;

            _diary = _diaryLoader.LoadDiary();
            _xmlView.SetRawXML(_diary.Raw);
            SetDiaryCurrentDay(DateTime.Now.AddDays(-1));
            _yesterdayView.SetDay(_diary.CurrentDay);
            SetDiaryCurrentDay(DateTime.Now.AddDays(-2));
            _twodaysView.SetDay(_diary.CurrentDay);

            SetDiaryCurrentDay(DateTime.Now);
            _dayView.SetDay(_diary.CurrentDay);

            DateTime dtMonth = new DateTime(_diary.CurrentYear.CurrentYear, _diary.CurrentMonth.CurrentMonth, 1);

            _bestMonthView.SetBest(_diary.CurrentMonth.Best, dtMonth.ToString("MMMM yyyy"));
            _bestYearView.SetBest(_diary.CurrentYear.Best, dtMonth.ToString("yyyy"));

            MainContent.Children.Add(_dayView);
            Grid.SetRow(_dayView, 0);
            Grid.SetColumn(_dayView, 1);
        }

        private void _monthView_OnSelectedDate(object sender, DateTime? date)
        {
            SetDiaryCurrentDay((DateTime)date);
            _monthView.SetDay(_diary.CurrentDay);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            switch(button.Content.ToString())
            {
                case "Today":
                    SetDiaryCurrentDay(DateTime.Now);
                    SwitchView(VIEWS.TODAY);
                    break;
                case "Yesterday":
                    SetDiaryCurrentDay(DateTime.Now.AddDays(-1));
                    SwitchView(VIEWS.YESTERDAY);
                    break;
                case "Two Days Ago":
                    SetDiaryCurrentDay(DateTime.Now.AddDays(-2));
                    SwitchView(VIEWS.TWODAYS);
                    break;
                case "Browse":
                    SwitchView(VIEWS.MONTH);
                    break;
                case "Year's Best":
                    SwitchView(VIEWS.BESTYEAR);
                    break;
                case "XML":
                    SwitchView(VIEWS.XML);
                    break;
                case "Month's Best":
                    SwitchView(VIEWS.BESTMONTH);
                    break;
                case "Overview":
                    SwitchView(VIEWS.OVERVIEW);
                    break;
            }
            
        }
    }
}
